<script>
function Super(name, age) {
  this.name = name
  this.age = age
}
Super.prototype.fn = function() {
  return this.name + ' ' + this.age
}
function Sub(...args) {
  // this -> Sub的实例this
  Super.call(this, ...args) // 第一步
}
Sub.prototype = Object.create(Super.prototype) // 第二步
Sub.prototype.constructor = Sub // 第三步

let s = new Sub('张三', 19)
console.log(s)
console.log(s.fn())

class Animal {
  // constructor() {}

  constructor(name) {
    this.name = name
    this.speed = 0
  }
  run(speed) {
    this.speed = speed
    console.log(`${this.name}以${this.speed}的速度奔跑`)
  }
  stop() {
    this.speed = 0
    console.log(`${this.name}静止。`)
  }
}

class Rabbit extends Animal {
  // constructor(...args) { // 默认的构造方法
  //   super(...args)
  //   // 构造方法做的事情就是创建实例的this
  // }

  // constructor(name, earLength) { // 错，没调用super()
  //   this.speed = 0
  //   this.name = name
  //   this.earLength = earLength
  // }

  constructor(earLength, name, speed) { // 对
    // console.log('abc') // 可以
    // console.log(this) // 不可以
    // 子类的构造方法里使用super()，必须在使用this之前调用
    super(name)
    this.speed = speed
    this.earLength = earLength
    // super(name) // 错
  }

  hide() {
    console.log(`${this.name}藏起来了。`)
  }
  stop() { // 覆盖父的stop方法
    super.stop()
    console.log(`${this.name}缩成一团。`)
    this.hide()
  }
}

const a = new Animal('旺财')
a.run(100)
a.stop()
const b = new Rabbit(30, '小白', 5)
b.run(10)
b.stop()
b.hide()


</script>